package user

import grails.test.*

class AuthControllerTests extends ControllerUnitTestCase {
    
	def redirectParams
	def userService
	
	protected void setUp() {
        super.setUp()
        controller.userService = userService
        controller.class.metaClass.redirect = { Map args -> redirectParams = args }
    }

    protected void tearDown() {
        super.tearDown()
        controller.userService = null
    }
    
    void testLogout() {
        controller.session['user'] =  "admin"
        assertNotNull controller.session.user
        controller.logout()        
        assertEquals "login", redirectParams.action
        assertNotNull controller.session
    }
    
    
	void testDoIt(){
		//Testes com login de Petiano
		mockDomain(Petiano, [new Petiano(id:1, nome: "Zé", sobrenome:"Da Silva", email: "ze@bol.com.br",
											idade:20, login: "zezinho", password: "lalala".encodeAsHash(), 
											admission: new Date()-1, CRE:8, 
											ehBolsista: true, periodo: 5) ] )
		//dados válidos		
		controller.params.username = "admin"
		controller.params.password = "teste123"
		controller.doIt()
		assertEquals "auth" , redirectParams.controller
		
		//dados não existentes
		controller.params.login = mockParams.login + "umLoginErrado"
		controller.params.password = mockParams.password + "umaSenhaErrada"
		controller.doIt()
		assertEquals "login" , redirectParams.action
		
		//dados em branco
		controller.params.login = ""
		controller.params.password = ""
		controller.doIt()
		assertEquals "login" , redirectParams.action
		
	}
	
}
